package com.personal.dmc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;


/**
 * @author Ray
 * @date 2013. 10. 10.
 * @description 초기설정을 및 Login
 */
public class IntroActivity extends Activity {

	private TextView introText;
	private TextView loginButton;
	private Session.StatusCallback statusCallback = new SessionStatusCallback();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_intro);

		introText = (TextView) findViewById(R.id.intro_text);
		
		loginButton = (TextView) findViewById(R.id.login_button);
		loginButton.setOnClickListener(loginClickListener);

		Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            }
        }
		
		initiate();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	}
	
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }


	/**
	 *********** Private Method ***********
	 */
	private void initiate() {

		/**
		 * @TODO 초기설정이 완료되면 Fade-In되면서 Login Button이 나타난다.
		 */
		loginButton.setVisibility(View.VISIBLE);
	}
	
    private void updateView() {
        Session session = Session.getActiveSession();
        if (session.isOpened()) {
        	introText.setText("session.getAccessToken() : " + session.getAccessToken());
            loginButton.setText("페이스북 로그아웃");
            loginButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) { onClickLogout(); }
            });
        } else {
        	introText.setText("Login to create a link to fetch account data");
            loginButton.setText("페이스북 로그인");
            loginButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) { onClickLogin(); }
            });
        }
    }
    
    private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        } else {
            Session.openActiveSession(this, true, statusCallback);
        }
    }

    private void onClickLogout() {
        Session session = Session.getActiveSession();
        if (!session.isClosed()) {
            session.closeAndClearTokenInformation();
        }
    }


	/**
	 *********** Listener ***********
	 */
	View.OnClickListener loginClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Session session = Session.getActiveSession();
	        if (!session.isOpened() && !session.isClosed()) {
	            session.openForRead(new Session.OpenRequest(IntroActivity.this).setCallback(statusCallback));
	        } else {
	            Session.openActiveSession(IntroActivity.this, true, statusCallback);
	        }
		}
	};
	
	private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            updateView();
        }
    }
}
